package com.ruoyi.apis.service.impl;

import com.ruoyi.apis.entity.OfflineEntity;
import com.ruoyi.apis.entity.VideoEntity;
import com.ruoyi.apis.repository.VideoRepository;
import com.ruoyi.apis.service.OfflineService;
import com.ruoyi.apis.service.VideoService;
import com.ruoyi.apis.vo.VideoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Choi
 * @Date 2021/7/19
 */

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoRepository videoRepository;

    @Override
    public VideoVO getById(long videoId) {
        VideoEntity videoEntity = videoRepository.findById(videoId);
        if (videoEntity == null) {
            return null;
        }
        return new VideoVO(videoEntity);
    }

    @Override
    public Page<VideoEntity> getVideos(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        return videoRepository.findAllByOrderBySortAsc(pageable);
    }

    @Override
    public List<VideoEntity> getByTitle(String title) {
        return videoRepository.findAllByTitleLikeOrderBySortAsc(title);
    }
}
